package com.yxjx.duoxue;

import java.util.ArrayList;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.tencent.android.tpush.XGPushClickedResult;
import com.tencent.android.tpush.XGPushManager;
import com.tencent.stat.StatService;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.yxjx.duoxue.comment.AddingCommentsActivity;
import com.yxjx.duoxue.comment.AddingCommentsActivity.PopupWindows;
import com.yxjx.duoxue.share.SharePopupWindow;
import com.yxjx.duoxue.utils.ShareUtils;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

public class BaseActionBarActivity extends ActionBarActivity {
	
	protected ActionBar mActionBar;
	protected int mActionBarLayoutId = -1;
	protected ArrayList<Integer> mActionBarIds = new ArrayList<Integer>();

	protected void customizeSearchActionBar(int resId) {
		mActionBar = getSupportActionBar();
		mActionBar.setCustomView(resId);
		mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		
		int[] ids = new int[] {
				R.id.back,
				R.id.favourite,
				R.id.share,
				R.id.login,
				R.id.messages
		};
		for (int id : ids) {
			UIHelper.setClickListener(mActionBar.getCustomView(), id, mBaseClickListener);
		}
	}
	
	@Override
	public void setContentView(int layoutResID) {
		customizeSearchActionBar(mActionBarLayoutId >= 0 ? mActionBarLayoutId : R.layout.head_basic);
		super.setContentView(layoutResID);
	}
	
	/**
	 * To be used by subclass, should be used before setContentView(int layoutId).
	 * @param layoutResId
	 */
	protected void setCustomActionBar(int layoutResId) {
		mActionBarLayoutId = layoutResId;
	}
	
	protected void customizeActionBarTitle(String str) {
		UIHelper.setText(mActionBar.getCustomView(), R.id.title, str);
	}
	
	protected void customizeActionBarTitle(int resId) {
		UIHelper.setText(mActionBar.getCustomView(), R.id.title, resId);
	}
	
	protected View.OnClickListener mBaseClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.back:
				UIHelper.hideIME(arg0);
				BaseActionBarActivity.this.onBackPressed();
				break;
			case R.id.share:
				SharePopupWindow popup = new SharePopupWindow(BaseActionBarActivity.this, findViewById(R.id.share));
				popup.setCallback(getShareCallback());
				break;
			default:
				break;
			}
		}
	};
	protected String mShareUrl;
	protected Object mShareObject;
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		StatService.onResume(this);
		XGPushClickedResult click = XGPushManager.onActivityStarted(this);
		if (click != null) {
			String custom = click.getCustomContent();
			Log.d("TPush", "custom = " + custom);
		}
	};
	
	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		StatService.onPause(this);
		XGPushManager.onActivityStoped(this);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initImageLoader(this);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
//		UIHelper.emptyDislpyedImageList();
		
		// imageloader stop
		ImageLoader.getInstance().stop();
	}
	
	/**
	 * Share功能的callback,subClass用该接口去定义share的行为
	 * @return
	 */
	public SharePopupWindow.Callback getShareCallback() {
		return new SharePopupWindow.Callback() {

			@Override
			public void onItemClick(int drawableId) {
				if (mShareObject == null || UIHelper.isEmpty(mShareUrl)) {
					return;
				}
				UMSocialService controller = ShareUtils.initShareController(BaseActionBarActivity.this, mShareUrl, mShareObject, drawableId);
				if (ShareUtils.drawableToChannel(drawableId) == null) {
					return;
				}
				controller.postShare(BaseActionBarActivity.this, ShareUtils.drawableToChannel(drawableId), new SnsPostListener() {
					
					@Override
					public void onStart() {
						// TODO Auto-generated method stub
						Utils.logd("postShare onStart");
					}
					
					@Override
					public void onComplete(SHARE_MEDIA arg0, int arg1, SocializeEntity arg2) {
						// TODO Auto-generated method stub
						Utils.logd("postShare onComplete " + arg0 + "," + "arg1" + "," + arg2);
					}
				});
			}
		};
	}
	
	// used by subclass.
	protected void customizeShare(String url, Object shareObject) {
		mShareUrl = url;
		mShareObject = shareObject;
	};
	
	public interface Observee {
		void notifyDataChanged();
	}
	
	public static void initImageLoader(Context context) {
		// This configuration tuning is custom. You can tune every option, you may tune some of them,
		// or you can create default configuration by
		//  ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.memoryCache(new WeakMemoryCache())
				.denyCacheImageMultipleSizesInMemory()
				.diskCacheFileNameGenerator(new Md5FileNameGenerator())
				.diskCacheSize(50 * 1024 * 1024) // 50 Mb
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
	}
	
	/**
	 * hide IME
	 * @param view
	 */
	protected final void hideIME(View view) {
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
		imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
	}
	
}
